<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Post Controller
 *
 * @package		SimplyPost
 * @subpackage	Post
 * @category	Controller
 * @author		Pascal Kriete
 */

class Post extends BaseController {

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Post()
	{
		parent::BaseController();
		$this->lang->load('post');
		$this->load->helper('date');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * No real function - redirect
	 *
	 * @access	public
	 */
	function index()
	{
		$this->session->set_flashdata('msg', 'Post does not exist.');
		redirect('/');
	}
	
	// --------------------------------------------------------------------

	/**
	 * Show and validate reply form
	 *
	 * @access	public
	 * @param	integer	topic id
	 */
	function reply($id)
	{		
		set_title($this->lang->line('post_reply_title'));
		$this->breadcrumb->from_db($id, 'topic', 'Reply');
		
		$this->load->helper('form');
		$this->load->library('validation');
		$this->load->library('post_handler');
				
		$data = array(
			'form_target'	=> 'post/reply/'.$id,
			'submit_text'	=> 'Reply',
			'success_msg'	=> 'Post Added'
		);

		$this->post_handler->add_post($id, $data);
	}
	
	// --------------------------------------------------------------------

	/**
	 * Edit a post
	 *
	 * @access	public
	 * @param	integer	post id
	 * @param	integer	parent id
	 */
	function edit($id, $parent_id)
	{
		set_title($this->lang->line('post_edit_title'));
		$this->breadcrumb->from_db($id, 'post', 'Edit');

		$this->load->model('Post_model', 'Post');

		$original = $this->Post->get_post($id);
	
		if ($this->_user['id'] !== $original['user_id'] && !$this->is_admin) {
			$this->session->set_flashdata('msg', 'You do not have permission to do this.');
			redirect('topic/view/'.$parent_id);
		}

		$content = $this->plugin->pre_edit($original['content']);
		$data = array(
			'id' 		=> $id,
			'parent_id' => $parent_id,
			'orig' 		=> $content
		);
	
		$this->load->helper('form');
		$this->load->library('validation');
	
		$rules['content'] = "trim|required|xss_clean|callback__plugins";
		$this->validation->set_rules($rules);

		$fields['content'] = 'Reply';
		$this->validation->set_fields($fields);

		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');
	
		if ($this->validation->run() == FALSE)
		{
			$this->load->view('edit', $data);
		}
		else
		{
			$db_data = array(
				'id'		=> $id,
				'content' 	=> $this->input->post('content')
			);
			$new_id = $this->Post->update($db_data);
		
			$this->session->set_flashdata('msg', 'Post Updated!');
			redirect('topic/view/'.$parent_id);
		}
	}
	
	// --------------------------------------------------------------------

	/**
	 * Validate new/edited post
	 *
	 * @access	public
	 * @param	string	post body
	 * @return	bool
	 */
	function _plugins($text)
	{
		$answer = $this->plugin->post_submit($text);
	
		if ($answer[0] === FALSE)
		{
			$this->validation->set_message('plugins', $answer[1]);
			return FALSE;
		}
		else
		{
			$_POST['content'] = $answer[1];
			return TRUE;
		}
	}

}